﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Sobiens.Web.Controls.ItemSelector.Samples.BLL
{
    public class ItemsManager
    {
        public static DataSet GetData()
        {
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("Items");
            dt.Columns.Add("Item_ID", typeof(int));
            dt.Columns.Add("ItemName");
            dt.Columns.Add("Description");
            dt.Columns.Add("Category");

            for (int i = 1; i < 21; i++)
            {
                DataRow newRow = dt.NewRow();
                newRow["Item_ID"] = i;
                newRow["ItemName"] = string.Format("Item{0}", i);
                newRow["Description"] = string.Format("Description{0}", i);
                newRow["Category"] = string.Format("Category{0}", i);
                dt.Rows.Add(newRow);
            }

            return ds;
        }

        public static DataSet SearchItems(string keyword)
        {
            DataSet ds = GetData();
            DataTable table = ds.Tables["Items"];

            // Presuming the DataTable has a column named Date.
            string expression;
            expression = string.Format("ItemName like '{0}%'", keyword);
            DataRow[] foundRows;

            // Use the Select method to find all rows matching the filter.
            foundRows = table.Select(expression);
            DataSet dsNew = new DataSet();
            DataTable dtNew = dsNew.Tables.Add("Items");
            dtNew.Columns.Add("Item_ID", typeof(int));
            dtNew.Columns.Add("ItemName");
            dtNew.Columns.Add("Description");
            dtNew.Columns.Add("Category");

            foreach(DataRow row in foundRows)
            {
                dtNew.Rows.Add(row.ItemArray);
            }
            return dsNew;
        }

    }
}